package 矩阵;

import java.util.Random;

public class 矩阵转置 {

    public static void main(String[] args) {
        int row = 5,cloum = 8;
        int[][] matrixA = creatMatrix(row,cloum);
        showMatrix(matrixA);
        int[][] matrixB = transposeMatrix(matrixA);
        showMatrix(matrixB);
    }

    static int[][] creatMatrix(int row, int column){
        Random random = new Random();  // 随机函数，默认当前时间为种子
        int[][] matrix = new int[row][column];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                matrix[i][j] = random.nextInt(100); // 范围 0-99
            }
        }
        return matrix;
    }
    static int[][] transposeMatrix(int[][] matrix){
        //二维数组本质是一维数组，行数=数组名.length,列数=数组名[0].length;
        int row = matrix.length, column = matrix[0].length;
        int[][] transposeMatrix = new int[column][row];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                transposeMatrix[j][i] = matrix[i][j]; // 范围 0-99
            }
        }
        return transposeMatrix;
    }
    static void showMatrix(int[][] matrix){
        int row = matrix.length, column = matrix[0].length;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                System.out.print(matrix[i][j]+"\t");
            }
            System.out.println();
        }
        System.out.println();
    }
}
